package com.xyz.service.impl;

import com.xyz.entity.vo.MessageNoticeVo;
import com.xyz.dto.SelectPageAllDto;
import com.xyz.dto.SelectPageUpkeepDto;
import com.xyz.entity.pojo.PatrolWorkOrder;
import com.xyz.entity.pojo.RepairWorkOrder;
import com.xyz.entity.pojo.UpkeepWorkOrder;
import com.xyz.service.MessageNoticeService;
import com.xyz.service.PatrolService;
import com.xyz.service.RepairAppService;
import com.xyz.service.UpkeepAppService;
import com.xyz.util.BeanUtils;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * author：
 * time: 2025/1/3 0003 18:24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MessageNoticeServiceImpl implements MessageNoticeService {

    @Autowired
    private RepairAppService repairAppService;
    @Autowired
    private UpkeepAppService upkeepAppService;
    @Autowired
    private PatrolService patrolService;

    @Override
    public DataResult selectUnreadMessages(LoginInfo li) throws Exception {
        MessageNoticeVo vo = new MessageNoticeVo();
        /*查询待审核模块的未处理消息*/
        //维修单
        SelectPageAllDto selectPageAllDto = new SelectPageAllDto();
        selectPageAllDto.setRepState("2");
        selectPageAllDto.setPageNum("1");
        selectPageAllDto.setPageSize("15");
        DataResult pendingRepairAll = repairAppService.getPendingRepairAll(li, selectPageAllDto);
        Object obj1 = pendingRepairAll.getInfo();
        List<RepairWorkOrder> repairWorkOrderList = new ArrayList<>();
        if (obj1 != null && obj1 instanceof GridData) {
            GridData gridData = (GridData) obj1;
            repairWorkOrderList = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto = new SelectPageUpkeepDto();
        selectPageUpkeepDto.setUpkeepState("2");
        selectPageUpkeepDto.setPageNum("1");
        selectPageUpkeepDto.setPageSize("15");
        DataResult pendingUpkeepAll = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto);
        Object obj2 = pendingUpkeepAll.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList = new ArrayList<>();
        if (obj2 != null && obj2 instanceof GridData) {
            GridData gridData = (GridData) obj2;
            upkeepWorkOrderList = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算待审核未处理消息总数
        vo.setUnreadMessages1(repairWorkOrderList.size() + upkeepWorkOrderList.size());

        /*查询待抢单模块的未处理消息*/
        //巡检单
        DataResult selectPatrolApp = patrolService.selectPatrolApp(li, 1, 15, "7", "", "",null ,null , null);
        Object obj3 = selectPatrolApp.getInfo();
        List<PatrolWorkOrder> patrolWorkOrderList = new ArrayList<>();
        if (obj3 != null && obj3 instanceof GridData) {
            GridData gridData = (GridData) obj3;
            patrolWorkOrderList = BeanUtils.toBean(gridData.getRows(), PatrolWorkOrder.class);
        }
        //维修单
        SelectPageAllDto selectPageAllDto1 = new SelectPageAllDto();
        selectPageAllDto1.setRepState("3");
        selectPageAllDto1.setPageNum("1");
        selectPageAllDto1.setPageSize("15");
        DataResult pendingRepairAll1 = repairAppService.getPendingRepairAll(li, selectPageAllDto1);
        Object obj4 = pendingRepairAll1.getInfo();
        List<RepairWorkOrder> repairWorkOrderList1 = new ArrayList<>();
        if (obj4 != null && obj4 instanceof GridData) {
            GridData gridData = (GridData) obj4;
            repairWorkOrderList1 = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto1 = new SelectPageUpkeepDto();
        selectPageUpkeepDto1.setUpkeepState("3");
        selectPageUpkeepDto1.setPageNum("1");
        selectPageUpkeepDto1.setPageSize("15");
        DataResult pendingUpkeepAll1 = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto1);
        Object obj5 = pendingUpkeepAll1.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList1 = new ArrayList<>();
        if (obj5 != null && obj5 instanceof GridData) {
            GridData gridData = (GridData) obj5;
            upkeepWorkOrderList1 = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算待抢单未处理消息总数
        vo.setUnreadMessages2(patrolWorkOrderList.size() + repairWorkOrderList1.size() + upkeepWorkOrderList1.size());

        /*查询待派单模块的未处理消息*/
        //维修单
        SelectPageAllDto selectPageAllDto2 = new SelectPageAllDto();
        selectPageAllDto2.setRepState("4");
        selectPageAllDto2.setPageNum("1");
        selectPageAllDto2.setPageSize("15");
        DataResult pendingRepairAll2 = repairAppService.getPendingRepairAll(li, selectPageAllDto2);
        Object obj6 = pendingRepairAll2.getInfo();
        List<RepairWorkOrder> repairWorkOrderList2 = new ArrayList<>();
        if (obj6 != null && obj6 instanceof GridData) {
            GridData gridData = (GridData) obj6;
            repairWorkOrderList2 = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto2 = new SelectPageUpkeepDto();
        selectPageUpkeepDto2.setUpkeepState("4");
        selectPageUpkeepDto2.setPageNum("1");
        selectPageUpkeepDto2.setPageSize("15");
        DataResult pendingUpkeepAll2 = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto2);
        Object obj7 = pendingUpkeepAll2.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList2 = new ArrayList<>();
        if (obj7 != null && obj7 instanceof GridData) {
            GridData gridData = (GridData) obj7;
            upkeepWorkOrderList2 = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算待派单未处理消息总数
        vo.setUnreadMessages3(repairWorkOrderList2.size() + upkeepWorkOrderList2.size());

        /*查询验收指派模块的未处理消息*/
        //维修单
        SelectPageAllDto selectPageAllDto3 = new SelectPageAllDto();
        selectPageAllDto3.setRepState("9");
        selectPageAllDto3.setPageNum("1");
        selectPageAllDto3.setPageSize("15");
        DataResult pendingRepairAll3 = repairAppService.getPendingRepairAll(li, selectPageAllDto3);
        Object obj8 = pendingRepairAll3.getInfo();
        List<RepairWorkOrder> repairWorkOrderList3 = new ArrayList<>();
        if (obj8 != null && obj8 instanceof GridData) {
            GridData gridData = (GridData) obj8;
            repairWorkOrderList3 = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto3 = new SelectPageUpkeepDto();
        selectPageUpkeepDto3.setUpkeepState("9");
        selectPageUpkeepDto3.setPageNum("1");
        selectPageUpkeepDto3.setPageSize("15");
        DataResult pendingUpkeepAll3 = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto3);
        Object obj9 = pendingUpkeepAll3.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList3 = new ArrayList<>();
        if (obj9 != null && obj9 instanceof GridData) {
            GridData gridData = (GridData) obj9;
            upkeepWorkOrderList3 = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算验收指派未处理消息总数
        vo.setUnreadMessages4(repairWorkOrderList3.size() + upkeepWorkOrderList3.size());

        /*查询待验收模块的未处理消息*/
        //维修单
        SelectPageAllDto selectPageAllDto4 = new SelectPageAllDto();
        selectPageAllDto4.setRepState("10");
        selectPageAllDto4.setPageNum("1");
        selectPageAllDto4.setPageSize("15");
        DataResult pendingRepairAll4 = repairAppService.getPendingRepairAll(li, selectPageAllDto4);
        Object obj10 = pendingRepairAll4.getInfo();
        List<RepairWorkOrder> repairWorkOrderList4 = new ArrayList<>();
        if (obj10 != null && obj10 instanceof GridData) {
            GridData gridData = (GridData) obj10;
            repairWorkOrderList4 = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto4 = new SelectPageUpkeepDto();
        selectPageUpkeepDto4.setUpkeepState("10");
        selectPageUpkeepDto4.setPageNum("1");
        selectPageUpkeepDto4.setPageSize("15");
        DataResult pendingUpkeepAll4 = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto4);
        Object obj11 = pendingUpkeepAll4.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList4 = new ArrayList<>();
        if (obj11 != null && obj11 instanceof GridData) {
            GridData gridData = (GridData) obj11;
            upkeepWorkOrderList4 = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算待验收未处理消息总数
        vo.setUnreadMessages4(repairWorkOrderList4.size() + upkeepWorkOrderList4.size());

        /*查询我处理的未处理消息*/
        //巡检单
        DataResult selectPatrolApp1 = patrolService.selectPatrolApp(li, 1, 15, "2,3,6", "", "", null,null , null);
        Object obj12 = selectPatrolApp1.getInfo();
        List<PatrolWorkOrder> patrolWorkOrderList1 = new ArrayList<>();
        if (obj12 != null && obj12 instanceof GridData) {
            GridData gridData = (GridData) obj12;
            patrolWorkOrderList1 = BeanUtils.toBean(gridData.getRows(), PatrolWorkOrder.class);
        }
        //维修单
        SelectPageAllDto selectPageAllDto5 = new SelectPageAllDto();
        selectPageAllDto5.setRepState("6");
        selectPageAllDto5.setPageNum("1");
        selectPageAllDto5.setPageSize("15");
        DataResult pendingRepairAll5 = repairAppService.getPendingRepairAll(li, selectPageAllDto5);
        Object obj13 = pendingRepairAll5.getInfo();
        List<RepairWorkOrder> repairWorkOrderList5 = new ArrayList<>();
        if (obj13 != null && obj13 instanceof GridData) {
            GridData gridData = (GridData) obj13;
            repairWorkOrderList5 = BeanUtils.toBean(gridData.getRows(), RepairWorkOrder.class);
        }
        //维保单
        SelectPageUpkeepDto selectPageUpkeepDto5 = new SelectPageUpkeepDto();
        selectPageUpkeepDto5.setUpkeepState("6");
        selectPageUpkeepDto5.setPageNum("1");
        selectPageUpkeepDto5.setPageSize("15");
        DataResult pendingUpkeepAll5 = upkeepAppService.getPendingUpkeepAll(li, selectPageUpkeepDto5);
        Object obj14 = pendingUpkeepAll5.getInfo();
        List<UpkeepWorkOrder> upkeepWorkOrderList5 = new ArrayList<>();
        if (obj14 != null && obj14 instanceof GridData) {
            GridData gridData = (GridData) obj14;
            upkeepWorkOrderList5 = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrder.class);
        }
        //计算我处理的消息总数
        vo.setUnreadMessages6(patrolWorkOrderList1.size() + repairWorkOrderList5.size() + upkeepWorkOrderList5.size());


        return DataResult.build9100(vo);
    }
}
